#include "stdafx.h"
#include "wf_drawarc3p.h"

#include "wf_drawarc.h"
#include "pf_commands.h"
#include "pf_snapper.h"
#include "pf_dialogfactory.h"



WF_DrawArc3P::WF_DrawArc3P(PF_Container& container,
                                       PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Draw arcs 3P",
                           container, graphicView) {
    reset();
}



WF_DrawArc3P::~WF_DrawArc3P() {}


QAction* WF_DrawArc3P::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Arc: 3 Points"
	QAction* action = new QAction(tr("&3 Points"), NULL);
	action->setIcon(QIcon(":/extui/arcs3p.png"));
 	//action->zetStatusTip(tr("Draw arcs with 3 points"));
		return action;
}


void WF_DrawArc3P::reset() {
    data.reset();
    point1 = PF_Vector(false);
    point2 = PF_Vector(false);
    point3 = PF_Vector(false);
}



void WF_DrawArc3P::init(int status) {
    PF_PreviewActionInterface::init(status);

    //reset();
}



void WF_DrawArc3P::trigger() {
    PF_PreviewActionInterface::trigger();

    preparePreview();
    if (data.isValid()) {
        PF_Arc* arc = new PF_Arc(container,
                                 data);
        arc->setLayerToActive();
        arc->setPenToActive();
        container->addEntity(arc);

        // upd. undo list:
        if (document!=NULL) {
            document->startUndoCycle();
            document->addUndoable(arc);
            document->endUndoCycle();
        }

		graphicView->redraw(LX_Define::RedrawDrawing);
        graphicView->moveRelativeZero(arc->getEndpoint());

        setStatus(SetPoint1);
        reset();
    } else {
        //PF_DIALOGFACTORY->requestWarningDialog(tr("Invalid arc data."));
        PF_DIALOGFACTORY->commandMessage(tr("Invalid arc data."));
    }
}



void WF_DrawArc3P::preparePreview() {
    data.reset();
    if (point1.valid && point2.valid && point3.valid) {
        PF_Arc arc(NULL, data);
        bool suc = arc.createFrom3P(point1, point2, point3);
        if (suc) {
            data = arc.getData();
        }
    }
}


void WF_DrawArc3P::mouseMoveEvent(PF_MouseEvent* e) {
    PF_Vector mouse = snapPoint(e);

    switch (getStatus()) {
    case SetPoint1:
        point1 = mouse;
        break;

    case SetPoint2:
        point2 = mouse;
        if (point1.valid) {
            PF_Line* line = new PF_Line(preview, PF_LineAttribute(point1, point2));

            deletePreview();
            preview->addEntity(line);
            drawPreview();
        }
        break;

    case SetPoint3:
        point3 = mouse;
        preparePreview();
        if (data.isValid()) {
            PF_Arc* arc = new PF_Arc(preview, data);

            deletePreview();
            preview->addEntity(arc);
            drawPreview();
        }
        break;

    default:
        break;
    }
}



void WF_DrawArc3P::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_DrawArc3P::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }
    PF_Vector mouse = e->getCoordinate();

    switch (getStatus()) {
    case SetPoint1:
        point1 = mouse;
        graphicView->moveRelativeZero(mouse);
        setStatus(SetPoint2);
        break;

    case SetPoint2:
        point2 = mouse;
        graphicView->moveRelativeZero(mouse);
        setStatus(SetPoint3);
        break;

    case SetPoint3:
        point3 = mouse;
        trigger();
        break;

    default:
        break;
    }
}



void WF_DrawArc3P::commandEvent(PF_CommandEvent* e) {
    PF_String c = e->getCommand().lower();

    if (checkCommand("help", c)) {
        PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                         + getAvailableCommands().join(", "));
        return;
    }

    if (PF_COMMANDS->checkCommand("center", c, rtti())) {
        finish();
        graphicView->setCurrentAction(
            new WF_DrawArc(*container, *graphicView));
    }
}



PF_StringList WF_DrawArc3P::getAvailableCommands() {
    PF_StringList cmd;
    return cmd;
}



void WF_DrawArc3P::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetPoint1:
        PF_DIALOGFACTORY->updateMouseWidget(
            tr("Specify startpoint or [Center]"),
            tr("Cancel"));
        break;
    case SetPoint2:
        PF_DIALOGFACTORY->updateMouseWidget(
            tr("Specify second point"), tr("Back"));
        break;
    case SetPoint3:
        PF_DIALOGFACTORY->updateMouseWidget(
            tr("Specify endpoint"), tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_DrawArc3P::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_DrawArc3P::updateToolBar() {
    if (!isFinished()) {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
    } else {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarArcs);
    }
}


// EOF

